package com.foreveross.action.weixin;

import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.action.BaseCostroller;
import com.foreveross.action.SysVisitorCheckInController;
import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.VisitorCheckInService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.VisitorCheckIn;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.weixin.service.QrcodeTicketService;
import com.foreveross.weixin.vo.Qrcode.ReturnQrcodeVo;
@Controller
@RequestMapping(value = "/weixin/visitorCheckInController")
public class VisitorCheckInController extends BaseCostroller {
	
	
	private Logger log = Logger.getLogger(VisitorCheckInController.class);
	@Resource
	private VisitorCheckInService visitorCheckInService;
	@Resource
	private LogService logService;
	
	/**  
	 * 访客登记列表
	 * @param request
	 * @param response
	 * @param currentPage
	 * @param pageSize
	 * @param weixinId
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findWeixinVisitorCheckIn")
	public void findWeixinVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, String currentPage,String pageSize,String weixinId) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) visitorCheckInService
					.getVisitorCheckInListCount(null, null, null,null, null,weixinId,null);
			PageVo<VisitorCheckIn> pageObj = new PageVo<VisitorCheckIn>(
					currentPage, countRecord, pageSize);
			pageObj.setData(visitorCheckInService.findVisitorCheckInList(null, null, null,null, null,weixinId, pageObj.getpageIndex(),
					pageObj.getpageSize(),null));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
			log.info("访客登记 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("访客登记  查看失败");
			log.error("访客登记 查看失败!", e);
		}
		List list = Arrays.asList("houseUser");
		CommonUtil.PrintWriterResultWithJSON(response, resultVo,list);
	}
	
	/**
	 * 访客登记添加修改
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateVisitorCheckIn")
	public void addOrUpdateVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, VisitorCheckIn visitorCheckIn,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			//暂时以UUID为二维码值
			String randomId = CommonUtil.getRandomString(10);
			ReturnQrcodeVo returnQrcodeVo = QrcodeTicketService.getQrcodeTicket(randomId);
			visitorCheckIn.setRandomId(randomId);
			visitorCheckIn.setTicket(returnQrcodeVo.getEncodeTicket());
			visitorCheckIn.setTicketId(returnQrcodeVo.getUrl());
			if(visitorCheckIn.getEndTime()!=null)
			{
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				DateFormat dfv = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String old = df.format(visitorCheckIn.getEndTime()) + " 23:59:59";
				visitorCheckIn.setEndTime(dfv.parse(old));
			}
			visitorCheckInService.saveOrUpdate(visitorCheckIn, updateType);
			resultVo.setSuccess(true);
			resultVo.setData(visitorCheckIn);
			log.info("访客登记 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("访客登记 " + updateType + "失败");
			log.error("访客登记" + updateType + "失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 查看访客登记
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findVisitorCheckIn")
	public void findVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, String id,String weixinId,String nickName) {
		ResultVo resultVo = new ResultVo();
		try {
			VisitorCheckIn visitorCheckIn = visitorCheckInService.getVisitorCheckIn(id, weixinId);
			resultVo.setSuccess(true);
			resultVo.setData(visitorCheckIn);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("查看访客登记失败");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 获取好友访客认证
	 * 
	 * @param request
	 * @param response
	 * @param id
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/friendVisitorCheckIn")
	public void friendVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			VisitorCheckIn hrHouses=visitorCheckInService.getVisitorCheckIn(id, null);
			resultVo.setSuccess(true);
			resultVo.setData(hrHouses);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("获取好友访客认证");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	
	/**
	 * 访客登记删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteVisitorCheckInList")
	public void deleteVisitorCheckInList(HttpServletRequest request,
			HttpServletResponse response, String id) {
		ResultVo resultVo = new ResultVo();
		try {
			visitorCheckInService.deleteObjecList("VisitorCheckIn", id);
			resultVo.setSuccess(true);
			log.info("访客登记 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("访客登记 删除失败");
			log.info("访客登记 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 验证ticketId是否为有效二维码
	 * 
	 * @param request
	 * @param response
	 * @param ticketId
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/scanVisitorCheckIn")
	public void scanVisitorCheckIn(HttpServletRequest request,
			HttpServletResponse response, String ticketId) {
		ResultVo resultVo = new ResultVo();
		try {
			VisitorCheckIn visitorCheckIn=visitorCheckInService.scanVisitorCheckIn(ticketId);
			
			if (visitorCheckIn.getVerifyStatus() == "1" || visitorCheckIn.getVerifyStatus().equals("1")){
				visitorCheckIn.setVerifyTime(new Date());
				visitorCheckIn.setVerifyStatus("0");
				visitorCheckInService.saveOrUpdate(visitorCheckIn, Constants.UPDATE_EDIT);
				// 由于此处返回给前端，展现为状态未失效。
				visitorCheckIn.setVerifyStatus("1");
			}
			resultVo.setSuccess(true);
			resultVo.setData(visitorCheckIn);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("验证二维码无效");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
